package me.leetcodepractice;

public class E02Leetcode203 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode s = new ListNode(-1, head); // 哨兵节点
        ListNode p1 = s;
        ListNode p2 = s.next;
        while (p2 != null) {
            if (p2.val == val) {
                p1.next = p2.next;
                p2 = p1.next;
            } else {
                p1 = p1.next;
                p2 = p1.next;
            }
        }
        return s.next;
    }

    public static void main(String[] args) {
        ListNode head = ListNode.of(1, 2, 6, 3, 6);
        System.out.println(head);
        System.out.println(new E02Leetcode203().removeElements(head, 6));
    }
}
